<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use clt\Leftnav;
class Sys extends Common
{
    /********************************数据库还原/备份*******************************/
    protected $db = '', $datadir =  './public/Data/';
    function _initialize(){
        parent::_initialize();
        $db=db('');
        $this->db =   db::connect();
    }
    public function database(){
        $dataList = $this->db->query("SHOW TABLE STATUS LIKE '".config('prefix')."%'");
        $total='';
        foreach ($dataList as $row){
            $total += $row['Data_length'];
        }
        $this->assign('totalSize', $total);
        $this->assign("dataList", $dataList);
        return $this->fetch();
    }
    public function backUp(){
        if(empty( $_POST['tables'])) {
            $dataList = $this->db->query("SHOW TABLE STATUS LIKE '".config('prefix')."%'");
            foreach ($dataList as $row){
                $table[]= $row['Name'];
            }
        }else{
            $table=$_POST['tables'];
        }
        $sql = "-- ".config('sys_name')." SQL Backup\n-- Time:".toDate(time())."\n-- http://www.cltphp.cn \n\n";
        foreach($table as $key=>$table) {
            $sql .= "--\n-- 表的结构 `$table`\n-- \n";
            $sql .= "DROP TABLE IF EXISTS `$table`;\n";
            $info = $this->db->query("SHOW CREATE TABLE  $table");
            $sql .= str_replace(array('USING BTREE','ROW_FORMAT=DYNAMIC'),'',$info[0]['Create Table']).";\n";
            $result = $this->db->query("SELECT * FROM $table ");
            if($result)$sql .= "\n-- \n-- 导出`$table`表中的数据 `$table`\n--\n";
            foreach($result as $key=>$val) {
                foreach ($val as $k=>$field){
                    if(is_string($field)) {
                        $val[$k] = '\''. $this->db->escape_string($field).'\'';
                    }elseif(empty($field)){
                        $val[$k] = 'NULL';
                    }
                }
                $sql .= "INSERT INTO `$table` VALUES (".implode(',', $val).");\n";
            }
        }
        $filename = empty($fileName)? date('YmdH').'_'.rand_string(10) : $fileName;
        $r= file_put_contents($this->datadir . $filename.'.sql', trim($sql));
        if($r){
            /*$this->assign ( 'jumpUrl', U(MODULE_NAME.'/recover') );
            $this->success(L('do_ok'));*/
            $this->redirect('sys/recover');
        }
    }
    public function recover(){
        if(input('do')=='delete'){
            foreach ((array)$_POST['files'] as $r){
                unlink($r);
            }
            $this->success("成功");
        }elseif(input('do')=='import'){
            header('Content-Type: text/html; charset=UTF-8');
            $path = $this->datadir.input('param.filename');
            if (!file_exists($path))
            {
                $this->error('SQL文件不存在!');
                return false;
            }
            else
            {
                $sql = $this->parseSQL($path);
                //dump($sql);die;
                try
                {
                    $this->handler->exec($sql);
                    echo '还原成功!花费时间', round(microtime(true) - $this->begin,2) . 'ms';
                }
                catch (PDOException $e)
                {
                    $this->error = $e->getMessage();
                    return false;
                }
            }
        }else{
            $filelist = dir_list($this->datadir);
            foreach ((array)$filelist as $r){
                $files[] = array('path'=> $r,'name' => basename($r), 'size' => filesize($r), 'time' => filemtime($r));
            }
            $this->assign('files',$files);
            return $this->fetch();
        }
    }
    /**
     * 解析SQL文件为SQL语句数组
     * @param string $path
     * @return array|mixed|string
     */
    private function parseSQL($path = '')
    {
        $sql = file_get_contents($path);
        $sql = explode("\r\n", $sql);
        //先消除--注释
        $sql = array_filter($sql, function ($data)
        {
            if (empty($data) || preg_match('/^--.*/', $data))
            {
                return false;
            }
            else
            {
                return true;
            }
        });
        $sql = implode('', $sql);
        //删除/**/注释
        $sql = preg_replace('/\/\*.*\*\//', '', $sql);
        return $sql;
    }
    /********************************站点管理*******************************/
    //站点设置
    public function sys(){
        $sys=db('sys')->where('sys_id',1)->find();
        $this->assign('sys',$sys);
        return $this->fetch();
    }
    //保存站点设置
    public function runsys(){
        $data=array(
            'sys_name'=>input('post.sys_name'),
            'sys_url'=>input('post.sys_url'),
            'sys_title'=>input('post.sys_title'),
            'sys_key'=>input('post.sys_key'),
            'sys_des'=>input('post.sys_des'),
            'bah'=>input('post.bah'),
            'copyright'=>input('post.copyright'),
            'ads'=>input('post.ads'),
            'tel'=>input('post.tel'),
            'email'=>input('post.email'),
        );
        db('sys')->where('sys_id',1)->update($data);
        $result = ['info' => '站点设置保存成功！', 'status' => '1'];
        return $result;
    }
    //站点设置
    public function wesys(){
        $sys=db('sys')->where('sys_id=1')->find();
        $this->assign('sys',$sys);
        return $this->fetch();
    }
    //保存微信设置
    public function addwei(){
        $sys=db('sys');
        $sl_data=array(
            'sys_id'=>input('post.sys_id'),
            'wesys_name'=>input('post.wesys_name'),
            'wesys_id'=>input('post.wesys_id'),
            'wesys_number'=>input('post.wesys_number'),
            'wesys_appid'=>input('post.wesys_appid'),
            'wesys_appsecret'=>input('post.wesys_appsecret'),
            'wesys_type'=>input('post.wesys_type'),
        );
        $sys->update($sl_data);
        $result['status'] = 1;
        $result['info'] = '微信设置保存成功!';
        $result['url'] = url('wesys');
        return $result;

    }
    /*-----------------------管理员管理----------------------*/
    //管理员列表
    public function adminList(){
        $val=input('val');
        $url['val'] = $val;
        $this->assign('testval',$val);
        $map='';
        if($val){
            $map['a.username|a.email|a.tel']= array('like',"%".$val."%");
        }
        if (session('aid')!=1){
            $map['a.admin_id']=session('aid');
        }

        $adminList=Db::table('clt_admin')->alias('a')
            ->join('clt_auth_group ag','a.group_id = ag.group_id','left')
            ->field('a.*,ag.title')
            ->where($map)
            ->paginate(config('pageSize'));
        $adminList->appends($url);
        // 模板变量赋值
        $page = $adminList->render();
        $this->assign('page', $page);
        $this->assign('admin_list',$adminList);
        return $this->fetch();
    }
    public function adminAdd(){
        $auth_group=db('auth_group')->select();
        $this->assign('auth_group',$auth_group);
        return $this->fetch();
    }
    public function adminInsert(){
        $admin = db('admin');
        $username = input('post.username');
        $check_user = $admin->where(array('username'=>$username))->find();
        if ($check_user) {
            $result['status'] = 0;
            $result['info'] = '用户已存在，请重新输入用户名!';
            return $result;
            exit;
        }
        $request = Request::instance();
        $data = array(
            'username' => $username,
            'pwd' => input('post.pwd', '', 'md5'),
            'email' => input('post.email'),
            'tel' => input('post.tel'),
            'open' => input('post.open'),
            'ip' => $request->ip(),
            'addtime' => time(),
            'group_id' => input('post.group_id')
        );
        $admin->insert($data);
        $result['status'] = 1;
        $result['info'] = '管理员添加成功!';
        $result['url'] = url('adminList');
        return $result;
    }
    //删除管理员
    public function adminDel(){
        $admin_id=input('get.admin_id');
        if (session('aid')==1){
            if (empty($admin_id)){
                $result['status'] = 0;
                $result['info'] = '用户ID不存在!';
                $result['url'] = url('adminList');
                exit;
            }
            db('admin')->where('admin_id='.$admin_id)->delete();
            $this->redirect('adminList');
        }
    }
    //修改管理员状态
    public function adminState(){
        $id=input('post.id');
        if (empty($id)){
            $result['status'] = 0;
            $result['info'] = '用户ID不存在!';
            $result['url'] = url('adminList');
            exit;
        }
        $status=db('admin')->where('admin_id='.$id)->value('is_open');//判断当前状态情况
        if($status==1){
            $data['is_open'] = 0;
            db('admin')->where('admin_id='.$id)->update($data);
            $result['status'] = 1;
            $result['info'] = '状态禁止';
        }else{
            $data['is_open'] = 1;
            db('admin')->where('admin_id='.$id)->update($data);
            $result['status'] = 1;
            $result['info'] = '状态开启';
        }
        return $result;
    }
    //更新管理员信息
    public function adminEdit(){
        $auth_group = db('auth_group')->select();
        echo input('get.admin_id');
        $info = db('admin')->where('admin_id='.input('admin_id'))->find();
        $this->assign('info', $info);
        $this->assign('auth_group', $auth_group);
        return $this->fetch();
    }
    public function adminUpdate(){
        $admin=db('admin');
        $pwd=input('post.pwd');
        $map['admin_id'] = array('neq',input('post.admin_id'));
        $where['admin_id'] = input('post.admin_id');
        if(input('post.username')){
            $map['username'] = input('post.username');
            $check_user = $admin->where($map)->find();
            if ($check_user) {
                $result['status'] = 0;
                $result['info'] = '用户已存在，请重新输入用户名!';
                exit;
            }
        }

        if ($pwd){
            $admindata['pwd']=input('post.pwd','','md5');
        }
        if(input('post.username')){
            $admindata['username']=input('post.username');
            $admindata['group_id']=input('post.group_id');
        }
        $admindata['email']=input('post.email');
        $admindata['tel']=input('post.tel');

        $admindata['open']=input('post.open');
        $admin->where($where)->update($admindata);
        $result['status'] = 1;
        $result['info'] = '管理员修改成功!';
        $result['url'] = url('adminList');
        return $result;
    }
    /*-----------------------用户组管理----------------------*/
    //用户组管理
    public function adminGroup(){
        $list=db('auth_group')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    //删除管理员分组
    public function groupDel(){
        db('auth_group')->where('group_id='.input('id'))->delete();
        $this->redirect('adminGroup');
    }
    //修改分组状态
    public function groupState(){
        $map['group_id']=input('post.id');
        $status=db('auth_group')->where($map)->value('status');//判断当前状态情况
        if($status==1){
            db('auth_group')->where($map)->setField('status',0);
            $result['info'] = '状态禁止';
        }else{
            db('auth_group')->where($map)->setField('status',1);
            $result['info'] = '状态开启';
        }
        $result['status'] = 1;
        return $result;
    }
    //添加分组
    public function groupAdd(){
        return $this->fetch();
    }
    public function groupInsert(){
        $auth_group=db('auth_group');
        $data=array(
            'title'=>input('post.title'),
            'status'=>input('post.status'),
            'addtime'=>time(),
        );
        $auth_group->insert($data);
        $result['info'] = '用户组添加成功!';
        $result['url'] = url('adminGroup');
        $result['status'] = 1;
        return $result;

    }
    //修改分组
    public function groupEdit(){
        $id = input('id');
        $group=db('auth_group')->where(array('group_id'=>$id))->find();
        $this->assign('group',$group);
        return $this->fetch();
    }
    public function groupUpdate(){
        $auth_group=db('auth_group');
        $data=array(
            'title'=>input('post.title'),
            'status'=>input('post.status')
        );
        $map['group_id'] = input('post.id');
        $auth_group->where($map)->update($data);
        $result['info'] = '用户组修改成功!';
        $result['url'] = url('adminGroup');
        $result['status'] = 1;
        return $result;
    }
    //分组配置规则
    public function groupAccess(){
        $admin_group=db('auth_group')->where(array('group_id'=>input('id')))->find();
        $authRule = db('auth_rule');
        $data = $authRule->field('id,name,title')->where(array('pid'=>0,'authopen'=>0))->order('sort')->select();
        foreach ($data as $k=>$v){
            $data[$k]['sub'] = $authRule->field('id,name,title')->where(array('pid'=>$v['id'],'authopen'=>0))->order('sort')->select();
            foreach ($data[$k]['sub'] as $kk=>$vv){
                $data[$k]['sub'][$kk]['sub'] = $authRule->field('id,name,title')->where(array('pid'=>$vv['id'],'authopen'=>0))->order('sort')->select();
                foreach ($data[$k]['sub'][$kk]['sub'] as $kkk=>$vvv){
                    $data[$k]['sub'][$kk]['sub'][$kkk]['sub'] =$authRule->field('id,name,title')->where(array('pid'=>$vvv['id'],'authopen'=>0))->order('sort')->select();
                }
            }
        }
        $this->assign('admin_group',$admin_group);	// 顶级
        $this->assign('datab',$data);	// 顶级
        return $this->fetch();
    }
    public function groupSetaccess(){
        $authGroup = db('auth_group');
        if(empty($_POST['new_rules'])){
            $this->error('请选择权限！',0,0);
        }
        $new_rules = $_POST['new_rules'];
        $imp_rules = implode(',', $new_rules).',';
        $map['group_id'] = input('id');
        $sldata=array(
            'rules'=>$imp_rules
        );
        if($authGroup->where($map)->update($sldata)){
            $result['info'] = '权限配置成功!';
            $result['url'] = url('groupAccess',array('id'=>input('id')));
            $result['status'] = 1;
            return $result;
        }else{
            $this->error('配置没有改变，无需保存',url('groupAccess',array('id'=>input('id'))),0);
        }
    }

    /********************************权限管理*******************************/
    public function adminRule(){
        $nav = new Leftnav();
        $admin_rule=db('auth_rule')->order('sort asc')->select();
        $arr = $nav->rule($admin_rule);
        $this->assign('admin_rule',$arr);//权限列表
        //dump($arr);
        return $this->fetch();
    }
    public function ruleAdd(){
        $admin_rule=db('auth_rule');
        $data=array(
            'name'=>input('post.name'),
            'title'=>input('post.title'),
            'status'=>1,
            'menustatus'=>input('post.status'),
            'sort'=>input('post.sort'),
            'addtime'=>time(),
            'pid'=>input('post.pid'),
            'css'=>input('post.css'),
        );
        $admin_rule->insert($data);
        $result['info'] = '权限添加成功!';
        $result['url'] = url('adminRule');
        $result['status'] = 1;
        return $result;
    }
    public function ruleorder(){
        $auth_rule=db('auth_rule');
        foreach ($_POST as $id => $sort){
            $auth_rule->where(array('id' => $id ))->setField('sort' , $sort);
        }
        $result['info'] = '排序更新成功!';
        $result['url'] = url('adminRule');
        $result['status'] = 1;
        return $result;
    }
    public function ruleState(){
        $id=input('post.id');
        $statusone=db('auth_rule')->where(array('id'=>$id))->value('menustatus');//判断当前状态情况
        if($statusone==1){
            $statedata = array('menustatus'=>0);
            db('auth_rule')->where(array('id'=>$id))->setField($statedata);
            $result['info'] = '状态禁止';
            $result['status'] = 1;
        }else{
            $statedata = array('menustatus'=>1);
            db('auth_rule')->where(array('id'=>$id))->setField($statedata);
            $result['info'] = '状态开启';
            $result['status'] = 1;
        }
        return $result;

    }
    public function ruleTz(){
        $id=input('post.id');
        $statusone=db('auth_rule')->where(array('id'=>$id))->value('authopen');//判断当前状态情况
        if($statusone==1){
            $statedata = array('authopen'=>0);
            db('auth_rule')->where(array('id'=>$id))->setField($statedata);
            $result['info'] = '需要验证';
            $result['status'] = 1;
        }else{
            $statedata = array('authopen'=>1);
            db('auth_rule')->where(array('id'=>$id))->setField($statedata);
            $result['info'] = '无需验证';
            $result['status'] = 1;
        }
        return $result;
    }

    public function ruleDel(){
        db('auth_rule')->where(array('id'=>input('id')))->delete();
        $this->redirect('adminRule');
    }
    public function ruleEdit(){
        $admin_rule=db('auth_rule')->where(array('id'=>input('id')))->find();
        $this->assign('rule',$admin_rule);
        return $this->fetch();
    }
    public function ruleUpdate(){
        $admin_rule=db('auth_rule');
        $map['id'] = input('post.id');
        $data=array(
            'name'=>input('post.name'),
            'title'=>input('post.title'),
            'status'=>1,
            'menustatus'=>input('post.status'),
            'css'=>input('post.css'),
            'sort'=>input('post.sort')
        );
        $admin_rule->where($map)->update($data);
        $result['info'] = '权限修改成功!';
        $result['url'] = url('adminRule');
        $result['status'] = 1;
        return $result;
    }

    static public function rule($cate , $lefthtml = '— ' , $pid=0 , $lvl=0, $leftpin=0 ){
        $arr=array();
        foreach ($cate as $v){
            if($v['pid']==$pid){
                $v['lvl']=$lvl + 1;
                $v['leftpin']=$leftpin + 0;//左边距
                $v['lefthtml']=str_repeat($lefthtml,$lvl);
                $arr[]=$v;
                $arr= array_merge($arr,self::rule($cate,$lefthtml,$v['id'],$lvl+1 , $leftpin+20));
            }
        }
        return $arr;
    }
}
